<template>
  <div class="app-container calendar-list-container">
    <div v-if="!opShow">
      <search :list="queryList" :queryEntity="queryEntity"></search>
      <buttons :showQuery="true" :showBack="false" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="" :permissions="permissions" @add="doAdd"></buttons>
      <grid :columns="columns" :multi="false" :url="queryUrl" ref="mainGrid" height="auto" diffHeight="105" @change="rowCilck" :queryEntity="queryEntity" :pagination="true"
        :permissions="permissions" @edit="doEdit" @view="doView" @open="doOpen" @close="doClose" @delete="doDelete"></grid>
    </div>
    <op ref="op" :visible="opShow" @cancel="cancel"></op>
  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import search from '@/components/Search'
import op from './op'
import { Message, MessageBox } from 'element-ui'
import { mapGetters } from 'vuex'
import { YesOrNoFormatter, DataAuthFormatter } from '@/const/dict'
import { dict } from '@/const/dict'

export default {
  components: { grid, buttons, op, search },
  data () {
    return {
      queryUrl: url.WORK_LIST_URL,
      disabled: true,
      opShow: false,
      positionCode: null,
      queryEntity: { key: 'key' },
      columns: [
        {
          label: '操作', width: 200, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '编辑', clickName: 'edit', auth: 'work_edit' },
            { name: '查看', clickName: 'view', auth: 'work_view' },
            { name: '删除', clickName: 'delete', auth: 'work_delete', confirm: true },
            { name: '开启', clickName: 'open', auth: 'work_open', confirm: true, valueProp: "status", showValue: [0] },
            { name: '关闭', clickName: 'close', auth: 'work_close', confirm: true, valueProp: "status", showValue: [1] }
          ]
        },
        { label: '待办标题', width: 160, prop: 'workTitle', align: 'left' },
        { label: '内容', width: 300, prop: 'workContent', align: 'center' },
        { label: '开始时间', width: 160, prop: 'workStartTime', align: 'center' },
        { label: '结束时间', width: 160, prop: 'workEndTime', align: 'center' },
        { label: '是否启用', width: 80, prop: 'status', align: 'center', formatter: YesOrNoFormatter },
        { label: '创建人', width: 100, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'work_add', ctr: false },
        //{ name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'work_edit', ctr: true },
        //{ name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'work_view', ctr: true },
        //  { name: '开启', type: 'primary', icon: 'edit', click: 'open', auth: 'work_open', ctr: true, confirm: true },
        //  { name: '关闭', type: 'primary', icon: 'edit', click: 'close', auth: 'work_close', ctr: true, confirm: true }
      ],
      queryList: [
        { name: '待办标题', type: 'text', prop: 'workTitleLike', placeholder: "待办标题" },
        { name: '是否启用', type: 'select', prop: 'status', placeholder: "是否启用", values: dict().YesOrNo, label: 'name', value: 'value' }
      ]
    }
  },
  created () {
  },
  computed: {
    ...mapGetters([
      'permissions'
    ])
  },
  methods: {
    doAdd () {
      this.opShow = true
      this.$refs['op'].init('新增', {}, false, url.WORK_ADD_URL)
    },
    doEdit (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.opShow = true
        this.$refs['op'].init('编辑', row, false, url.WORK_EDIT_URL)
      }
    },
    doView (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.opShow = true
        this.$refs['op'].init('查看', row, true)
      }
    },
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    cancel () {
      this.opShow = false
    },
    deleteOp (code) {
      return request({
        url: url.WORK_DELETE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'workCode': code }
      })
    },
    openOp (code) {
      return request({
        url: url.WORK_OPEN_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'workCode': code }
      })
    },
    closeOp (code) {
      return request({
        url: url.WORK_CLOSE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'workCode': code }
      })
    },
    doDelete (row) {
      //const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.deleteOp(row.workCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doOpen (row) {
      //const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.openOp(row.workCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doClose (row) {
      //  const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.closeOp(row.workCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
